package com.liuhong.shoping.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuhong.shoping.dao.UserMapper;
import com.liuhong.shoping.entity.User;
import com.liuhong.shoping.vo.out.LoginBean;
import com.liuhong.shoping.vo.out.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuhong
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserMapper userMapper;

    @PostMapping("/login")
    public ResultBean<LoginBean> login(@RequestBody User user) {
        QueryWrapper<User> queryWrapperUser = new QueryWrapper<>();
        queryWrapperUser.lambda().eq(User::getUsername,user.getUsername()).eq(User::getPassword,user.getPassword());
        User result = userMapper.selectOne(queryWrapperUser);
        if (result !=null){
            LoginBean lb = new LoginBean();
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            lb.setToken(uuid);
            lb.setId(result.getId());
            return new ResultBean(lb);
        }
        return new ResultBean(10001,"用户名密码错误");
    }
}

